﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Cms.Domain
{
    [Serializable]
    public class DetailTemplate : Template
    {
        private IList<DetailTemplateContentLink> _content;
        private MasterTemplate _defaultMasterTemplate;
        private IList<string> _areas;
        private IList<string> _pageAreas;

        public DetailTemplate()
        {
            _areas = new List<string>();
            _pageAreas = new List<string>();
            _content = new List<DetailTemplateContentLink>();
        }

        public virtual IList<string> Areas
        {
            get { return _areas; }
            set { _areas = value; }
        }

        public virtual IList<string> PageAreas
        {
            get { return _pageAreas; }
            set { _pageAreas = value; }
        }

        public virtual MasterTemplate DefaultMasterTemplate
        {
            get { return _defaultMasterTemplate; }
            set { _defaultMasterTemplate = value; }
        }

        public virtual IList<DetailTemplateContentLink> Content
        {
            get { return _content; }
            set { _content = value; }
        }
        
        public override string GetPublishedContentHtml(string areaID, int localeID)
        {
            foreach (DetailTemplateContentLink dtl in _content.Where(dtl => dtl.AreaID == areaID))
            {
                return dtl.Content.GetPublishedHtml(localeID);
            }
            return string.Empty;
        }
        
        public override string GetPublishedContentTitle(string areaID, int localeID)
        {
            foreach (DetailTemplateContentLink dtl in _content.Where(dtl => dtl.AreaID == areaID))
            {
                return dtl.Content.GetPublishedTitle(localeID);
            }
            return string.Empty;
        }
        
        public override string GetContentHtml(string areaID, int localeID, string timestamp)
        {
            foreach (DetailTemplateContentLink dtl in _content.Where(dtl => dtl.AreaID == areaID))
            {
                return dtl.Content.GetHtml(localeID, timestamp);
            }
            return string.Empty;
        }

        public override string GetContentTitle(string areaID, int localeID, string timestamp)
        {
            foreach (DetailTemplateContentLink dtl in _content.Where(dtl => dtl.AreaID == areaID))
            {
                return dtl.Content.GetTitle(localeID, timestamp);
            }
            return string.Empty;
        }
        
        public override Content GetContent(string areaID)
        {
            return (from dtl in _content where dtl.AreaID == areaID select dtl.Content).FirstOrDefault();
        }

        public override void AddContentLink(string areaID, Content content)
        {
            IList<DetailTemplateContentLink> links =
                _content.Where(existingLink => existingLink.AreaID == areaID).ToList();
            foreach (DetailTemplateContentLink existingLink in links)
            {
                // Remove all versions associated with the old content from the sets
                foreach (var kvp in ContentSets)
                {
                    ContentSet set = kvp.Value;
                    IList<ContentTranslationVersion> removableVersions = new List<ContentTranslationVersion>();
                    foreach (ContentTranslationVersion version in set.TranslationVersions)
                    {
                        if (version.Translation.Content.ID == existingLink.Content.ID)
                            removableVersions.Add(version);
                    }
                    foreach (ContentTranslationVersion version in removableVersions)
                        set.TranslationVersions.Remove(version);
                }
                _content.Remove(existingLink);
            }
            var link = new DetailTemplateContentLink
                           {
                               Content = content,
                               AreaID = areaID,
                               DetailTemplate = this
                           };
            _content.Add(link);
            content.DetailTemplates.Add(link);
        }

        public override void PublishContentVersion(int localeID, ContentTranslationVersion ctv)
        {
            if (ContentSets.ContainsKey(localeID))
                ContentSets[localeID].PublishedVersion = ctv;
            foreach (ContentTranslation translation in from detailTemplateLink in _content
                                                       where
                                                           detailTemplateLink.Content.Translations.ContainsKey(localeID)
                                                       select detailTemplateLink.Content.Translations[localeID])
            {
                translation.PublishVersion(ctv.Timestamp);
            }
        }

        protected override ContentSet CreateContentSet(Locale locale)
        {
            return new DetailTemplateContentSet(this, locale);
        }
    }
}